package com.optimatch.model.io;

import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import com.optimatch.ui.model.Model;
import com.optimatch.ui.model.beans.Probleme;

public class ReadXMLTeams {

	/**
	 * lit les �quipes d'un fichier xml et les int�gre dans un probl�me
	 * @param problem le probl�me dans lequel on veut ajouter les �quipes
	 * @param path l'adresse du fichier xml contenant les �quipes
	 */
	public static void readTeams(String path, Model model){

		try {
			model.setProbleme(new Probleme());
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			DocumentBuilder db;

			db = dbf.newDocumentBuilder();

			Document dom;
			dom = db.parse(path);

			Element docEle = dom.getDocumentElement();

			NodeList nlTeams = docEle.getElementsByTagName("equipe");

			for(int i = 0; i < nlTeams.getLength(); i++){
				Element elTeam = (Element) nlTeams.item(i);

				NodeList nlName = elTeam.getElementsByTagName("nom");
				Element elName = (Element) nlName.item(0);
				String name = elName.getFirstChild().getNodeValue();

				NodeList nlCity = elTeam.getElementsByTagName("ville");
				Element elCity = (Element) nlCity.item(0);
				String city = elCity.getFirstChild().getNodeValue();

				model.ajouterEquipe(city, name);
			}

			NodeList nlDaysOblig = docEle.getElementsByTagName("journeeObligatoire");

			for(int i = 0; i < nlDaysOblig.getLength(); i++){
				Element elDay = (Element) nlDaysOblig.item(i);

				int numJour = Integer.parseInt(elDay.getAttribute("num"));

				NodeList nlMatchs = elDay.getElementsByTagName("match");

				for(int j=0;j<nlMatchs.getLength();j++){
					Element elMatch = (Element) nlMatchs.item(j);

					NodeList nlDom = elMatch.getElementsByTagName("a_domicile");
					Element elDom = (Element) nlDom.item(0);
					boolean domicile = elDom.getFirstChild().getNodeValue().equals("1");

					NodeList nlTeam1 = elMatch.getElementsByTagName("equipe1");
					Element elTeam1 = (Element) nlTeam1.item(0);
					String team1 = elTeam1.getFirstChild().getNodeValue();

					NodeList nlTeam2 = elMatch.getElementsByTagName("equipe2");
					Element elTeam2 = (Element) nlTeam2.item(0);
					String team2 = elTeam2.getFirstChild().getNodeValue();

					int nTeam1 = model.getProbleme().getListeEquipes().indexOf(team1);
					int nTeam2 = model.getProbleme().getListeEquipes().indexOf(team2);

					model.fixerDateMatch(numJour, nTeam1, nTeam2, domicile);
				}
			}

			NodeList nlDaysPeriod = docEle.getElementsByTagName("journee");

			for(int i = 0; i < nlDaysPeriod.getLength(); i++){
				Element elDay = (Element) nlDaysPeriod.item(i);

				int numJourDeb = Integer.parseInt(elDay.getAttribute("debut"));
				int numJourFin = Integer.parseInt(elDay.getAttribute("fin"));

				NodeList nlMatchs = elDay.getElementsByTagName("match");

				for(int j=0;j<nlMatchs.getLength();j++){
					Element elMatch = (Element) nlMatchs.item(j);

					NodeList nlDom = elMatch.getElementsByTagName("a_domicile");
					Element elDom = (Element) nlDom.item(0);
					boolean domicile = elDom.getFirstChild().getNodeValue().equals("1");

					NodeList nlTeam1 = elMatch.getElementsByTagName("equipe1");
					Element elTeam1 = (Element) nlTeam1.item(0);
					String team1 = elTeam1.getFirstChild().getNodeValue();

					NodeList nlTeam2 = elMatch.getElementsByTagName("equipe2");
					Element elTeam2 = (Element) nlTeam2.item(0);
					String team2 = elTeam2.getFirstChild().getNodeValue();

					int nTeam1 = model.getProbleme().getListeEquipes().indexOf(team1);
					int nTeam2 = model.getProbleme().getListeEquipes().indexOf(team2);

					model.fixerPeriodeMatch(numJourDeb, numJourFin, nTeam1, nTeam2, domicile);
				}
			}


		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	public static void main(String[] args){
		Probleme p = new Probleme();
		//readTeams(p,"res/probleme.xml");
	}

}
